
redis.call("SELECT", 2)

local count = tonumber(ARGV[1])
local currEpochMilli = tonumber(ARGV[2])
local epochMilliBeforeMinutes = tonumber(ARGV[3])
local ip = ARGV[4]
local member = ARGV[5]

--- 当前 IP 不存在时，直接保存且返回 true
if redis.call("ZCARD", ip) == nil then
    redis.call("ZADD", ip, currEpochMilli, member)
    return true
end

--- 判断滑动窗口内的请求数是否大于请求限制数，大于直接返回 false，反之返回 true 且保存
local requestCount = #redis.call("ZRANGEBYSCORE", ip, epochMilliBeforeMinutes, currEpochMilli)
if requestCount < count then
    redis.call("ZADD", ip, currEpochMilli, member)
    return true
end
return false
